package cn.jieruoda.eb.system;

import org.springframework.web.bind.annotation.*;
import cn.jieruoda.eb.system.service.MenuService;
import cn.jieruoda.eb.system.domain.entity.SystemMenu;
import cn.jieruoda.eb.system.dto.MenusResponse;
import cn.dev33.satoken.util.SaResult;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/menus")
public class MenuController {
    final MenuService menuService;

    @GetMapping
    public MenusResponse list() {
        return menuService.initMenus();
    }

    @PostMapping
    public SaResult create(@RequestBody SystemMenu menu) {
        return menuService.createMenu(menu);
    }

    @PutMapping("/{id}")
    public SaResult update(@PathVariable Integer id, @RequestBody SystemMenu menu) {
        menu.setId(id);
        return menuService.updateMenu(menu);
    }

    @DeleteMapping("/{id}")
    public SaResult delete(@PathVariable Integer id) {
        return menuService.deleteMenu(id);
    }

    @PutMapping("/{id}/move")
    public SaResult move(@PathVariable Integer id, @RequestParam Integer pid, @RequestParam Integer sort) {
        return menuService.moveMenu(id, pid, sort);
    }
}
